package sentinel.demo;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * @date: 2023/12/19 11:06
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
//        System.setProperty("csp.sentinel.config.file", "sentinel.properties");
//        initSentinelRule();
        SpringApplication.run(Application.class);
    }

    private static void initSentinelRule() {
        // 资源限流
        FlowRule flowRule = new FlowRule("test")
                .setCount(1)
                .setGrade(RuleConstant.FLOW_GRADE_QPS);
        List<FlowRule> list = new ArrayList<>();

        list.add(flowRule);
        FlowRuleManager.loadRules(list);

        // 异常降级
    /*List<DegradeRule> DegradeRules = new ArrayList<>();
    DegradeRule degradeRule = new DegradeRule("");
    degradeRule.setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType());
    degradeRule.setCount(0.7); // Threshold is 70% error ratio
    degradeRule.setMinRequestAmount(100)
            .setStatIntervalMs(30000) // 30s
            .setTimeWindow(10);
    DegradeRules.add(degradeRule);
    DegradeRuleManager.loadRules(DegradeRules);*/

        // 系统负载保护
    /*List<SystemRule> systemRules = new ArrayList<>();
    SystemRule systemRule = new SystemRule();
    systemRule.setHighestSystemLoad(10);
    systemRules.add(systemRule);
    SystemRuleManager.loadRules(systemRules);*/

        // 黑白名单授权访问
    /*AuthorityRule rule = new AuthorityRule();
    rule.setResource("test");
    rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
    rule.setLimitApp("appA,appB");
    AuthorityRuleManager.loadRules(Collections.singletonList(rule));*/
    }
}
